// promise 链式写法
function p1() {
  return new Promise((resolve, reject) => {
    resolve('p1')
  })
}

function p2(data) {
  return new Promise((resolve, reject) => {
    resolve('p2' + data)
  })
}

function p3() {
  return new Promise((resolve, reject) => {
    reject('p3')
  })
}

function fuc() {
  return p1().then(res1 => {
    return p2().then(res2 => {
      return res1 + res2
    })
  })
}
fuc().then(res => console.log(res)) // p1p2undefined

// 改为链式调用
function fun1() {
  return p1()
    .then(res1 => {
      return p2(res1)
    })
    .then(res => res)
}
fun1().then(res => console.log(res)) // p2p1

// 用catch捕获错误

function fun3() {
  return p1()
    .then(res => p2(res))
    .then(res => res)
    .then(res => p3())
    .then(res => res) // 接受p3的resolve

}
fun3().then(res => console.log(res))
  .catch(err => console.log('this is a error', err)) // this is a error p3
